


import { ccs } from "../../../util/services";
import { figureType, giftType, relationType } from "../../gameType";
import figureModel from "../../model/figureModel";
import { game } from "../game";
import { figureBase } from "../Item/ItemBase";
import { BagBase, BagData } from "./bagBase";

export class figureBag extends BagBase<figureBase>{
    bagData: BagData = {buyNum: 0, items: [], itemCount: 0};
    constructor(data) {
        super(
            { buyNum: 0, items: [], itemCount: 50 },
            data.itemTypes,
            data.bagType
        );
    }
    loadBag(bags) {
        this.bag.items = bags;
        //加载本地数据背包
       bags.forEach(val => {
           this._items[val.id] = new figureBase(val);
       })
    }
    addAge() {
        //年龄增长
        let hero: figureModel = null;
        this.bag.items.forEach(val => {
            hero = new figureModel(val.relation, val.hero);
            hero.addYear();
            // hero.promotion();
            //学习变化
            hero.calSchool();
        })
    }
    getRelation(relation: string): figureBase[] {
        let list = [];
        for(let key in this._items) {
            if(this._items[key].data.relation == relation) {
               list.push(this._items[key]);
            }
        }
        return list;
    }
    addMonth() {
        let hero: figureModel = null;
        let money = 0;
        this.bag.items.forEach(val => {
            hero = new figureModel(val.relation, val.hero);
            if(hero.heroInfo.lifeTime <= 0) {return};
            hero.reduceTime(-1);
            if(hero.heroInfo.lifeTime == 0) {
                game.recordMgr.addList({action: `${hero.heroInfo.name}去世了，默哀`});
                return ;
            }
            hero.addExp();
            // hero.randomCost();//随机消费
            if(val.relation != "friend") {
                hero.relationDisease();
            }
            if(hero.heroInfo.work) {
                hero.heroInfo.money += hero.getWork().income;
            } else {
                hero.randomWork();
            }
            if(hero.heroInfo.disease.length > 0 ) {
                // game.recordMgr.addList({action: `请及时带亲人${hero.heroInfo.name}去看病，避免病情恶化`});
                hero.diseaseReduceTime();
            }
            if(val.relation == "son") {
                money += hero.getChildPay();
            }
            hero.reduceMonth();
        })
        if(money > 0) {
            game.playerMgr.calMoney(-money);
            game.recordMgr.addList({action: `支出孩子的抚养费用${money}`});
        }
        
    }
    //增加亲密度
    addIntimate(id: string | number, gift: giftType) {
        let item = this.getItem(id);
        let figure: relationType = item.data;
        if(Math.random() >= 0.3 && figure.hero.money * 2 <= gift.cost) {
            figure.intimate += gift.effect;
            ccs.winMgr.showWin("toast", {msg: "亲密度增加" + gift.effect})
        }else {
            ccs.winMgr.showWin("toast", {msg: "对方收了你的礼物，但却不是很高兴"});
        }
    }
    //要求 向父母 要钱等行为
    require(id: string | number, gift: giftType): boolean {
        let item = this.getItem(id);
        let figure: figureBase = item;
        if(Math.random() >= 0.2) {
            figure.data.intimate += gift.effect;
            return true;
        }
        return false;
    }
    

}